package comm_test;

import java.io.IOException;
import java.net.UnknownHostException;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import comm.ClientInterface;
import comm.MoodType;
import comm.ServerInterface;


/**
 * 
 * This class puts to the test the following:
 * 
 * ---If the client can check that messages are pending 
 * ---If the client can get the messages sent by the server 
 * 
 */

public class ClientInterfaceMessageInspectorsTest {
	public static final String HOST = "localhost";
	public static final String CLIENT_1_NICKNAME = "monkey";

	private ServerInterface serverInterface;
	private ClientInterface clientInterface;

	@Before
	public void setUp() throws Exception {
		serverInterface = new ServerInterface();
		Assert.assertNotNull(serverInterface);
		clientInterface = new ClientInterface();
		Assert.assertNotNull(clientInterface);
	}

	@After
	public void tearDown() throws Exception {
		serverInterface = null;
		clientInterface = null;
	}
	
	/**
	 * This test is used to test the following:
	 * 
	 * 1 - Get the next message received from the Server. If no message 
	 *  has been received, the method blocks until a message has been received. 
	 * 
	 *		@method ClientSideMessage getNextMessage()
	 * 
	 * 
	 * 2 - Check if a message from the server is pending.
	 * 
	 *		@method boolean hasNextMessage()
	 */
	@Test
	public void  test_NextMessage() throws InterruptedException, UnknownHostException, IOException{
		System.out.println("\n\n*** BEGIN TEST ***");
		System.out.println("**@class=ClientInterfaceMessageInspectorsTest @method=getNextMessage()\n**");

		//Preparing Test.. Giving Time for resources to be free
		Thread.sleep(100);

		serverInterface.start();

		//connects client with server
		clientInterface.connect(HOST, CLIENT_1_NICKNAME);

		//(this) process needs to sleep to give time for processes(client and server)to exchange messages
		Thread.sleep(10);

		//server sends message
		serverInterface.sendMoodishMessage("Bartolo", CLIENT_1_NICKNAME, MoodType.Disgruntled.toString());
		serverInterface.sendMoodishMessage("Malakas", CLIENT_1_NICKNAME, MoodType.Careless.toString());

		//(this) process needs to sleep to give time for processes(client and server)to exchange messages
		Thread.sleep(10);

		Assert.assertTrue("client should have next message", clientInterface.hasNextMessage());
		clientInterface.getNextMessage();
		Assert.assertNotNull("it should NOT be null",clientInterface.getNextMessage());
		clientInterface.getNextMessage();
		Assert.assertFalse("client should NOT have next message", clientInterface.hasNextMessage());
		Assert.assertNull("it should be null",clientInterface.getNextMessage());

		serverInterface.sendMoodishMessage("Daimonas", CLIENT_1_NICKNAME, MoodType.Good.toString());

		//(this) process needs to sleep to give time for processes(client and server)to exchange messages
		Thread.sleep(10);

		Assert.assertTrue("client should have next message", clientInterface.hasNextMessage());
		clientInterface.getNextMessage();
		Assert.assertFalse("client shouldnt have next message", clientInterface.hasNextMessage());
		Assert.assertNull("it should be null",clientInterface.getNextMessage());

		//(this) process needs to sleep to give time for processes(client and server)to exchange messages
		Thread.sleep(10);
		//disconnects client
		clientInterface.disconnect();		
		serverInterface.shutdown();

		//Preparing Test.. Giving Time for other processes to finish
		Thread.sleep(100);
		System.out.println("\n**@class=ClientInterfaceMessageInspectorsTest @method=getNextMessage()**");
		System.out.println("*** END TEST ***\n");
	}

}
